(function(exports) {
    "use strict";
    var dataHandling = {
        numberToEn: function(number) {
            var str;
            switch (number) {
                case 1:
                    str = "one";
                    break;
                case 2:
                    str = "two";
                    break;
                case 3:
                    str = "three";
                    break;
                case 4:
                    str = "four";
                    break;
                default:
                    str = "one";
            }
            return str;
        },
        /**
         * [getKLength 返回K线数量]
         * @param  {[type]} numberStr [description]
         * @return {[type]}           [description]
         */
        getKLength:function(numberStr){
            var len = 0;
            switch(numberStr){
                case "01":
                len = 100;
                break;
                case "03":
                len = 200;
                break;
                case "06":
                len = 500;
                break;
                default:
                len = 500;             
            }
            return len;
        },
        /**
         * [numberToIndex 根据自定义月份展示菜单选择栏]
         * @param  {[type]} numberStr [description]
         * @return {[type]}           [description]
         */
        numberToIndex:function(numberStr){
            var index;
            switch(numberStr){
                case "01":
                index = 0;
                break;
                case "03":
                index = 1;
                break;
                case "06":
                index = 2;
                break;
                default:
                index = 0;             
            }
            return index;
        },
        len: function(str){
            var chineseRegex = /[^\x00-\xff]/g;
            return str.replace(chineseRegex,"**").length;
        },
        cutstr: function(str,len,char) {
            str = str.toString();
            var newLength = 0;
            var newStr = "";
            var chineseRegex = /[^\x00-\xff]/g;
            var singleChar = "";
            var strLength = str.replace(chineseRegex,"**").length;
            for(var i = 0;i < strLength;i++) {
                singleChar = str.charAt(i).toString();
                if(singleChar.match(chineseRegex) != null) {
                    newLength += 2;
                }else {
                    newLength++;
                }
                if(newLength > len) {
                    break;
                }
                newStr += singleChar;
            }
            if(strLength > len) {
                newStr += char;
            }
            return newStr;
        },        
        /**
         * [getPieData 提供数据给圆和页面图形展示]
         * @param  {[type]} dataArray [description]
         * @return {[type]}           [description]
         */
        getPieData: function(dataArray) {
            var count = 0;
            var result = [];
            var pieColor = ["#8BB8ED", "#FD6D70", "#F9B34B", "#C5C5C5"];
            for (var i = 0; i < dataArray.length; i++) {
                if (i > 3) {
                    break;
                }
                if (i === 3) {
                    result.push({
                        value: 100 - count,
                        color: pieColor[i],
                        name: '其它行业'
                    });
                } else {
                    count = count + parseFloat(dataArray[i].percent);
                    result.push({
                        value: parseFloat(dataArray[i].percent),
                        name:  this.cutstr(dataArray[i].name,8,"..."),
                        color: pieColor[i]
                    });
                }
            }
            return result;
        },
        /**
         * [getPieDataList 返回所有的持仓股票类型]
         * @param  {[type]} dataArray [description]
         * @return {[type]}           [description]
         */
        getPieDataList: function(dataArray) {
            var data = [];
            dataArray.forEach(function(item) {
                data.push(item.num + " 只为" + item.name);
            });
            return data.join(", ");
        },
        /**
         * [getIndexFromMostArray 获取最新的买卖点的索引]
         * @param  {[type]} dataArray [description]
         * @return {[type]}           [description]
         */
        getIndexFromMostArray: function(list,key) {
            for (var i = 0; i < list.length; i++) {
                if (list[i][0] == key) {
                    return i;
                    break;
                }
            }
        },
        /**
         * [getFuse 获取融字]
         * @return {[type]} [description]
         */
        getFuse: function(source) {
            if (source === "150002") {
                return true;
            } else {
                return false;
            }
        },
        /**
         * [getFuse 返回幅度]
         * @return {[type]} [description]
         */
        getRange: function(number) {
            var str = '';
            if (number > 0) {
                str = '+' + number;
            } else {
                str = number;
            }
            str = str + '%'
            return str;
        },
        //日期格式化
        formatDate:function(str,type){
            var date = "";
            switch(type){
                case 0:
                date = str.replace(/(\d{2})(\d{2})[\/|-](\d{2})[\/|-](\d{2})/gi, "$2/$3/$4");
                break;
                case 1:
                date = str.replace(/(\d{4})[\/|-](\d{2})[\/|-](\d{2})/gi, "$1$2$3");
                break;
                case 2:
                date = str.replace(/(\d{2})\/(\d{2})\/(\d{2})/gi, "20$1$2$3");
                break;
                case 3:
                date = str.replace(/(\d{4})(\d{2})(\d{2})/gi, "$1/$2/$3"); 
                break;
                case 4:
                date = str.replace(/(\d{2})(\d{2})(\d{2})(\d{2})/gi, "$2/$3/$4");  
                break;                                    
                default:
                date = str.replace(/(\d{2})(\d{2})[\/|-](\d{2})[\/|-](\d{2})/gi, "$2/$3/$4");
            }
            return date;
        },
        /**
         * [getCurrency 返回币种单位]
         * @return {[type]} [description]
         */
        getCurrency: function(currency) {
            var unit = '';
            switch (currency) {
                case "401156":
                    unit = '元';
                    break;
                case "401344":
                    unit = '港币'
                    break;
                case "401840":
                    unit = '美元'
                    break;
                default:
                    unit = '元';
            }
            return unit;
        },
        getShareClass:function(str){             
            var number = parseFloat(str);
            var className = "";
            if(isNaN(number)){
                className = " gray";
            }else if(number>0){
                className = " red";
            }else{
                className = " loss"
            }
            return className;
        },
        /**
         * [countLines 判断内容行数]
         * @param  {[type]} ele [description]
         * @return {[type]}     [description]
         */
        countLines: function(ele) {
            var styles = window.getComputedStyle(ele, null);
            var lh = parseInt(styles.lineHeight, 10);
            var h = parseInt(styles.height, 10);
            var lc = Math.round(h / lh);
            console.log('line count:', lc, 'line-height:', lh, 'height:', h);
            return lc;
        },        
        /**
         * [percentOptimize 收益分析柱形百分比展示]
         * @param  {[type]} maxValue     [description]
         * @param  {[type]} currentValue [description]
         * @return {[type]}              [description]
         */
        percentOptimize: function(maxValue,currentValue) {            
            var range = 3;
            var className = "";
            var dvalue = maxValue - range;
            if( currentValue < maxValue ){
                if( currentValue > maxValue - range ){
                    currentValue = dvalue==0? "1px" : dvalue + "%";
                }else{
                    currentValue = currentValue + "%";    
                }
                className = "";
            }else{
                currentValue = currentValue + "%";
                className = " radius-t";
            }
            return {
                className:className,
                value: currentValue
            };
        },        
        /**
         * [subPixelOptimize 亚像素优化]
         * @param  {[type]} position  [description]
         * @param  {[type]} lineWidth [description]
         * @return {[type]}           [description]
         */
        subPixelOptimize: function(position, lineWidth) {
            if (lineWidth % 2 === 1) {
                //position += position === Math.ceil(position) ? 0.5 : 0;
                position = Math.floor(position) + 0.5;
            } else {
                position = Math.round(position);
            }
            return position;
        }          
    };
    exports.dataHandling = dataHandling;
    /**
     * 兼容AMD和非AMD规范
     */
    if (typeof define === "function" && define.amd) {
        define([], function() {
            return dataHandling;
        });
    }
})(window);